import {
	defineStore
} from 'pinia';
import DictApi from '@/ya/api/dict.js';

const appConfig = defineStore('appConfig', {
	state: () => {
		const defTab = [{
			pagePath: '/pages/mine/mine',
			text: '我的',
			activeIcon: '/static/images/tabbar/mine_.png',
			icon: '/static/images/tabbar/mine.png',
			componentName: 'setting',
			name: "mineRef",
			props: {}
		}];
		// 配置项映射关系
		const configMapping = {
			open_time_start: ['open_time', 'start_time'],
			open_time_end: ['open_time', 'end_time'],
			exam_name: ['name_info', 'exam_name'],
			host_org: ['name_info', 'host_org'],
			photo_url: ['name_info', 'photo_url']
		};
		const settingConfig = {
			open_time: {
				start_time: '',
				end_time: ''
			},
			name_info: {
				exam_name: '',
				host_org: '',
				photo_url: ''
			}
		};
		return {
			defTab,
			dynamicTabConfig: [...defTab],
			configMapping,
			settingConfig
		};
	},
	actions: {
		async getDictData (){
			try {
				const res = await DictApi.getDictByType('system_config');
				if (res.code === 200) {
					// 解析字典数据到配置对象
					res.data.forEach(item => {
						const path = this.configMapping[item.bdKey];
						if (path) {
							this.settingConfig[path[0]][path[1]] = item.bdValue;
						}
					});
					console.log('settingConfig',this.settingConfig)
				}
			} catch (e) {
				uni.showToast({
					title: '配置加载失败',
					icon: 'none'
				});
			}
		},
		async setTabsByRole(loginType, userType) {
			let baseTabs = [...this.defTab];
			switch (loginType) {
				// 考生
				case 1:
					baseTabs.unshift({
						pagePath: '/pages/exam/examList',
						text: '考试',
						activeIcon: '/static/images/tabbar/exam_.png',
						icon: '/static/images/tabbar/exam.png',
						componentName: 'examList',
						name: "examListRef",
						props: {}
					});
					break;
					// 巡考
				case 2:
					baseTabs.unshift({
						pagePath: '/pages/exam/examList',
						text: '签到',
						activeIcon: '/static/images/tabbar/sign_.png',
						icon: '/static/images/tabbar/sign.png',
						componentName: 'examList',
						name: "examListRef",
						props: {}
					});
					break;
					// 管理员
				case 3:
					switch (userType) {
						// 中心管理员
						case 1:
							baseTabs.unshift({
								pagePath: '/pages/examManager/examManager',
								text: '签到详情',
								activeIcon: '/static/images/tabbar/sign_.png',
								icon: '/static/images/tabbar/sign.png',
								componentName: 'examManager',
								name: "examManagerRef",
								props: {}
							});
							break;
							// 考点管理员
						case 2:
							baseTabs.unshift({
								pagePath: '/pages/manager/signManage',
								text: '签到',
								icon: '/static/images/tabbar/sign.png',
								activeIcon: '/static/images/tabbar/sign_.png',
								componentName: 'signManage',
								name: "signManageRef",
								props: {}
							}, {
								pagePath: '/pages/manager/notice',
								text: '公告',
								activeIcon: '/static/images/tabbar/notice_.png',
								icon: '/static/images/tabbar/notice.png',
								componentName: 'notice',
								name: "noticeRef",
								props: {}
							});
							break;
							// 超级管理员
						case 99:
							baseTabs.unshift({
								pagePath: '/pages/admin/examSite',
								text: '考点',
								icon: '/static/images/tabbar/examsite.png',
								activeIcon: '/static/images/tabbar/examsite_.png',
								componentName: 'examSite',
								name: "examSiteRef",
								props: {}
							}, {
								pagePath: '/pages/admin/exportData',
								text: '导入',
								icon: '/static/images/tabbar/exportData.png',
								activeIcon: '/static/images/tabbar/exportData_.png',
								componentName: 'manageData',
								name: "manageDataRef",
								props: {}
							});
							break;
					};
					break
			};
			this.dynamicTabConfig = baseTabs;
		},
		async loginAfter() {
			// 登录成功后跳转第一页面
			const url = this.dynamicTabConfig[0].pagePath;
			uni.redirectTo({
				url: url
			});
		}
	}
});
export default appConfig